using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using PSE.Hosting.ServiceHoster.Services;
using System.Threading;
using PSE.Hosting.ServiceHoster.Business.Processes;

namespace PSE.Hosting.ServiceHoster
{
    /// <summary>
    /// The Windows Service class.
    /// </summary>
    public partial class WcfHoster : ServiceBase
    {
        #region Fields

        ServiceHost _servicesManagerHost;
        ServiceHosterProcessManager _processManager;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of <see cref="PSE.Hosting.ServiceHoster.WcfHoster"/>
        /// class.
        /// </summary>
        public WcfHoster()
        {
            InitializeComponent();
        }

        #endregion

        #region Protected Members

        /// <summary>
        /// Starts the service.
        /// </summary>
        /// <param name="args">Input arguments.</param>
        protected override void OnStart(string[] args)
        {
            bool startAll = true;

            if (args.Length.Equals(0))
            {
                // Fires a thread to start the services asynchronously.
                Thread thread = new Thread(new ParameterizedThreadStart(this.Run));
                thread.Start(startAll);
            }
            else
            {
                bool asynchronous = true;

                if (args.Length == 1)
                {
                    Boolean.TryParse(args[0], out asynchronous);
                }

                if (args.Length == 2)
                {
                    Boolean.TryParse(args[0], out asynchronous);
                    Boolean.TryParse(args[1], out startAll);
                }

                if (asynchronous)
                {
                    // Fires a thread to start the services asynchronously.
                    Thread thread = new Thread(new ParameterizedThreadStart(this.Run));
                    thread.Start(startAll);
                }
                else
                {
                    // Starts the thread synchronously;
                    this.Run(startAll);
                }
            }
        }

        /// <summary>
        /// Starts all WCF assemblies configured in the Service Hoster Manager.
        /// </summary>
        public void Run(object startAll)
        {
            _processManager = ServiceHosterProcessManager.GetInstance();
            _processManager.Initialize("ServiceHoster");

            if (Convert.ToBoolean(startAll))
            {
                // Start services.
                ServicesManager.StartAll();
            }

            // Start management service.
            _servicesManagerHost = new ServiceHost(typeof(ServicesManager));
            _servicesManagerHost.Open();
        }

        /// <summary>
        /// Stops the service.
        /// </summary>
        protected override void OnStop()
        {
            // Stop management service.
            _servicesManagerHost.Close();

            // Stop services.
            ServicesManager.StopAll();

            _processManager.Dispose();
        }

        #endregion
    }
}
